package com.homegoods.classify.ui.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.homegoods.classify.R;
import com.homegoods.classify.databinding.ClassifyFragmentClassifyBinding;
import com.homegoods.aroutercomment.ARouterPath;
import com.homegoods.resource.HomeGoodsApp;
import com.jakewharton.rxbinding2.view.RxView;
import com.kedll.library.adapter.AbFragmentPagerAdapter;
import com.kedll.library.adapter.MyArrayAdapter;
import com.kedll.library.fragment.KBaseFragment;
import com.kedll.library.mvp.BasePresenter;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author QiQi
 * @ClassName: ClassifyFragment
 * @date 2018/9/8
 * @description
 */
/*@Route(path = ARouterPath.PATH_FRAGMENT_CLASSIFY)*/   //del shibo.zheng
public class ClassifyFragment extends KBaseFragment<ClassifyFragmentClassifyBinding, BasePresenter> {
    private final static String[] STRS = {"品牌"};
    private ClassifyAdapter adapter;
    private int currentPosition = 0;

    @Override
    protected void initEvent(Bundle savedInstanceState) {
        adapter = new ClassifyAdapter(getActivity(), STRS);
        mBinding.list.setAdapter(adapter);

        List<Fragment> fragments = new ArrayList<>();
        fragments.add((Fragment) ARouter.getInstance().build(ARouterPath.PATH_CLASSIFY).navigation());
//        for (int i = 1; i < STRS.length; i++) {
//            Fragment fragment = (Fragment) ARouter.getInstance().build(ARouterPath.PATH_CLASSIFY_CLASSIFY).withString("type", STRS[i]).navigation();
//            fragments.add(fragment);
//        }

        AbFragmentPagerAdapter fragmentPagerAdapter = new AbFragmentPagerAdapter(getChildFragmentManager(), fragments);

        mBinding.viewpager.setAdapter(fragmentPagerAdapter);
        mBinding.list.setOnItemClickListener((parent, view, position, id) -> {
            if (currentPosition == position) {
                return;
            }
            currentPosition = position;
            adapter.setPosition(position);
            mBinding.viewpager.setCurrentItem(position, false);
        });
        mBinding.viewpager.setPagingEnabled(false);
        mBinding.viewpager.setOffscreenPageLimit(STRS.length);
        mBinding.viewpager.setCurrentItem(0, false);

        RxView.clicks(mBinding.searchBar)
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .compose(bindToLifecycle())
                .subscribe(o -> ARouter.getInstance()
                        .build(ARouterPath.PATH_SEARCH).navigation());

    }

    @Override
    protected BasePresenter getPresenter() {
        return null;
    }

    @Override
    protected int getLayout() {
        return R.layout.classify_fragment_classify;
    }

    private static class ClassifyAdapter extends MyArrayAdapter<String> {

        private int position = 0;

        ClassifyAdapter(Context context, String[] objects) {
            super(context, android.R.layout.simple_list_item_1, objects);
        }


        void setPosition(int position) {
            this.position = position;
            notifyDataSetChanged();
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            TextView view = (TextView) super.getView(position, convertView, parent);
            view.setGravity(Gravity.CENTER);
            view.setTextSize(12f);
            view.setPadding(2, 0, 0, 0);
            if (this.position == position) {
                view.setCompoundDrawablesRelativeWithIntrinsicBounds(R.mipmap.rectangle, 0, 0, 0);
                view.setBackgroundResource(R.color.white);
                view.setTextColor(ContextCompat.getColor(getContext(), R.color.color_a541c9));
            } else {
                view.setBackground(null);
                view.setCompoundDrawables(null, null, null, null);
                view.setTextColor(ContextCompat.getColor(getContext(), R.color.color_333333));
            }

            return view;
        }
    }

}
